/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CategoryPanel.java
 *
 * Created on 2012-feb-26, 12:54:15
 */
package dkgg2012group22imat.view.shop;

import dkgg2012group22imat.controller.shop.CategoriesController;
import dkgg2012group22imat.model.Category;
import dkgg2012group22imat.controller.shop.CategoryPanelController;
import dkgg2012group22imat.model.IMatUtilities;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JPanel;

/**
 *
 * @author Simon
 */
public class CategoryPanel extends javax.swing.JPanel {

    private CategoryPanelController controller;
    private Category category;
    private int level;
    private boolean active = false;
    private boolean clickable = true;
    private boolean hasSubs = false;
    private boolean hover = false;

    /** Creates new form CategoryPanel */
    public CategoryPanel(CategoriesController parent, Category category) {
        this(parent, category, 1);
    }

    public CategoryPanel(CategoriesController parent, Category category, int level) {
        initComponents();
        this.category = category;
        this.controller = new CategoryPanelController(parent, this);
        this.hasSubs = category.getSubCategories().size() > 0;
        this.categoryButton.setToolTipText("Visa kategorin " + category.getName());
        this.setLevel(level);
    }

    public Category getCategory() {
        return this.category;
    }

    public JButton getCategoryButton() {
        return this.categoryButton;
    }

    public JPanel getSubCategoriesPanel() {
        return this.subCategoriesPanel;
    }

    public void setLevel(int level) {
        this.level = level;
        this.categoryButton.setPreferredSize(new Dimension(250 - (30 * (level - 1)), 30));
    }

    public int getLevel() {
        return level;
    }

    public void hideSubCategories() {
//       IMatUtilities.animate(this.jPanel1,this.jPanel1.getSize(),new Dimension(0,0));
        this.jPanel1.setVisible(false);
    }

    public void showSubCategories() {
        this.jPanel1.setVisible(true);
        this.revalidate();
        this.repaint();
    }

    public void setActive(boolean active, boolean clickable) {

        this.active = active;
        this.clickable = clickable;

        this.updateState();

    }

    public void updateState() {
        if (this.active) {
            if (this.clickable) {
                this.categoryButton.setIcon(this.categoryButton.getRolloverIcon());
                this.categoryButton.setEnabled(true);
            } else {
                this.categoryButton.setEnabled(false);
            }
            this.setHoverText(true);
            if (this.hasSubs) {
                this.showSubCategories();
            }
        } else {
            this.categoryButton.setEnabled(true);
            this.categoryButton.setIcon(this.categoryButton.getDisabledSelectedIcon());
            this.setHoverText(hover);
            this.hideSubCategories();
        }
        this.revalidate();
        this.repaint();
    }

    public void setHoverText(boolean hover) {
        if (hover) {
            this.categoryButton.setText("<html><font color=white>" + this.getCategory().getName() + "</font></html>");
        } else {
            this.categoryButton.setText("<html><font color=black>" + this.getCategory().getName() + "</font></html>");
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        categoryButton = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        subCategoriesPanel = new javax.swing.JPanel();
        filler1 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(30, 0), new java.awt.Dimension(32767, 32767));
        filler2 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 10), new java.awt.Dimension(32767, 32767));

        setName("Form"); // NOI18N
        setOpaque(false);
        setLayout(new java.awt.BorderLayout());

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(dkgg2012group22imat.view.DKGG2012Group22iMatApp.class).getContext().getResourceMap(CategoryPanel.class);
        categoryButton.setBackground(resourceMap.getColor("categoryButton.background")); // NOI18N
        categoryButton.setFont(resourceMap.getFont("categoryButton.font")); // NOI18N
        categoryButton.setForeground(resourceMap.getColor("categoryButton.foreground")); // NOI18N
        categoryButton.setIcon(resourceMap.getIcon("categoryButton.icon")); // NOI18N
        categoryButton.setText(resourceMap.getString("categoryButton.text")); // NOI18N
        categoryButton.setBorder(javax.swing.BorderFactory.createLineBorder(resourceMap.getColor("categoryButton.border.lineColor"))); // NOI18N
        categoryButton.setDisabledIcon(resourceMap.getIcon("categoryButton.disabledIcon")); // NOI18N
        categoryButton.setDisabledSelectedIcon(resourceMap.getIcon("categoryButton.disabledSelectedIcon")); // NOI18N
        categoryButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        categoryButton.setMargin(new java.awt.Insets(0, 0, 0, 0));
        categoryButton.setMaximumSize(new java.awt.Dimension(248, 28));
        categoryButton.setMinimumSize(new java.awt.Dimension(0, 28));
        categoryButton.setName("categoryButton"); // NOI18N
        categoryButton.setOpaque(true);
        categoryButton.setPressedIcon(resourceMap.getIcon("categoryButton.pressedIcon")); // NOI18N
        categoryButton.setRolloverIcon(resourceMap.getIcon("categoryButton.rolloverIcon")); // NOI18N
        categoryButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseExited(java.awt.event.MouseEvent evt) {
                categoryButtonMouseExited(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                categoryButtonMouseEntered(evt);
            }
        });
        categoryButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                categoryButtonActionPerformed(evt);
            }
        });
        add(categoryButton, java.awt.BorderLayout.NORTH);

        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setOpaque(false);
        jPanel1.setLayout(new java.awt.BorderLayout());

        subCategoriesPanel.setName("subCategoriesPanel"); // NOI18N
        subCategoriesPanel.setOpaque(false);
        subCategoriesPanel.setLayout(new javax.swing.BoxLayout(subCategoriesPanel, javax.swing.BoxLayout.Y_AXIS));
        jPanel1.add(subCategoriesPanel, java.awt.BorderLayout.CENTER);

        filler1.setName("filler1"); // NOI18N
        jPanel1.add(filler1, java.awt.BorderLayout.WEST);

        filler2.setName("filler2"); // NOI18N
        jPanel1.add(filler2, java.awt.BorderLayout.SOUTH);

        add(jPanel1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

private void categoryButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_categoryButtonActionPerformed
    controller.open();
}//GEN-LAST:event_categoryButtonActionPerformed

private void categoryButtonMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_categoryButtonMouseEntered
    this.hover = true;
    this.updateState();
}//GEN-LAST:event_categoryButtonMouseEntered

private void categoryButtonMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_categoryButtonMouseExited
    this.hover = false;
    this.updateState();
}//GEN-LAST:event_categoryButtonMouseExited
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton categoryButton;
    private javax.swing.Box.Filler filler1;
    private javax.swing.Box.Filler filler2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel subCategoriesPanel;
    // End of variables declaration//GEN-END:variables
}
